package cn.pkubangbang.demo.democontact.service.impl;

import cn.pkubangbang.demo.democontact.entity.Blog;
import cn.pkubangbang.demo.democontact.entity.Page;
import cn.pkubangbang.demo.democontact.entity.PageInfo;
import cn.pkubangbang.demo.democontact.entity.Person;
import cn.pkubangbang.demo.democontact.mapper.BlogMapper;
import cn.pkubangbang.demo.democontact.service.BlogService;
import cn.pkubangbang.demo.democontact.service.PersonService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: zhangzhenbang@k2data.com.cn
 * @Date: 2018-09-06 17:52
 */
@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private PersonService personService;

    @Override
    public List<Blog> listBlogsOfPerson(Person p) {
        return blogMapper.getBlogsOfPerson(p);
    }

    @Override
    public Page<Blog> pageBlogsOfPerson(Person p, PageInfo pageInfo) {
        Long startIndexInclusive = pageInfo.getStartIndexInclusive();
        Integer pageSize = pageInfo.getPageSize();
        List<Blog> blogs = blogMapper
            .getBlogsOfPersonPaged(p, startIndexInclusive, new Long(pageSize));

        return new Page<>(pageInfo, blogs);
    }

    @Override
    public List<Blog> listAllBlogsForPersonAndHisFriends(Person p) {
        List<Person> people = personService.queryFriends(p);
        // add himself in
        people.add(p);

        return blogMapper.getBlogsOfPeople(people);

    }

    @Override
    public Page<Blog> pageAllBlogsForPersonAndHisFriends(Person p, PageInfo pageInfo) {
        List<Person> people = personService.queryFriends(p);
        people.add(p);

        Long startIndexInclusive = pageInfo.getStartIndexInclusive();
        Integer pageSize = pageInfo.getPageSize();

        List<Blog> blogs = blogMapper
            .getBlogsOfPeoplePaged(people, startIndexInclusive, new Long(pageSize));

        return new Page<>(pageInfo, blogs);
    }

    @Override
    public void postBlog(Blog blog) {
        blogMapper.insertBlog(blog);
    }
}
